%     VIRTUAL HC - программа для получения эффективных свойств сотовых заполнителей
%     Copyright (C) 2018-2020  Михаил Козлов
%     Сайт проекта: <http://www.fastenerdoctor.ru/>
%     Адрес электронной почты для связи с разработчиком:
%     <doctor.fastener@gmail.com>
% 
%     Эта программа является свободным программным обеспечением: Вы можете
%     распространять ее и/или изменять, соблюдая условия Универсальной общественной
%     лицензии GNU, опубликованной Фондом свободного программного обеспечения,
%     либо редакции 3, либо (на Ваше усмотрение) любой редакции, выпущенной позже.
% 
%     Эта программа распространяется в надежде на то, что она окажется полезной,
%     но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, включая даже подразумеваемую гарантию ТОВАРНОЙ ПРИГОДНОСТИ,
%     либо ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Ознакомьтесь с Универсальной общественной
%     лицензией GNU для получения более подробной информации.
% 
%     Вы должны были получить копию Универсальной общественной лицензии GNU
%     вместе с этой программой. Если Вы ее не получили, то перейдите по адресу:
%     <http://www.gnu.org/licenses/>.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <https://www.gnu.org/licenses/>.

function [NODES_2D, ELEMENTS_2D, Min_Element_Edge, N_Layers_Suggestion] = Mesher_2D_Type_3(RVE_Type, RVE_Frame_Points, Cell_Mesh_Type, N_Elements_Edge, Element_Critical_Ratio, Foil_Thickness, Cell_Filler)

global ErrCode;

global Merge_Tolerance_Multiplier;

NODES_2D = [];
ELEMENTS_2D = [];
Min_Element_Edge = [];
N_Layers_Suggestion = [];

Next_Node_Number = 1;
Next_Element_Number = 1;

switch Cell_Mesh_Type
    
    case 'SHELL'
        
        switch RVE_Type
            
            case 'TYPE_A'
                
                % Число элементов на вертикальных и горизонтальных линиях ячейки
                N_Elements_Horizontal = N_Elements_Edge;
                N_Elements_Vertical = N_Elements_Edge;
                
                % Контроль соотношения сторон элементов в наполнителе
                if (strcmp(Cell_Filler, 'TRUE'))
                    
                    Element_Ratio = (norm(RVE_Frame_Points(2,:) - RVE_Frame_Points(6,:)) / N_Elements_Vertical) / ...
                                    (norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(2,:)) / N_Elements_Horizontal);
                    
                    if (Element_Ratio > Element_Critical_Ratio)
                        N_Elements_Vertical = ceil(N_Elements_Vertical * Element_Ratio / Element_Critical_Ratio);
                    end
                    
                    Element_Ratio = (norm(RVE_Frame_Points(5,:) - RVE_Frame_Points(6,:)) / N_Elements_Horizontal) / ...
                                    (norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(5,:)) / N_Elements_Vertical);
                    
                    if (Element_Ratio > Element_Critical_Ratio)
                        N_Elements_Horizontal = ceil(N_Elements_Horizontal * Element_Ratio / Element_Critical_Ratio);
                    end
                    
                end
                
                % Минимальная длина ребра элемента
                Min_Element_Edge = min([norm(RVE_Frame_Points(5,:) - RVE_Frame_Points(6,:)) / N_Elements_Horizontal, ...
                                        norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(2,:)) / N_Elements_Horizontal, ...
                                        norm(RVE_Frame_Points(2,:) - RVE_Frame_Points(6,:)) / N_Elements_Vertical, ...
                                        norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(5,:)) / N_Elements_Vertical]);
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(6,:), RVE_Frame_Points(5,:), N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 1);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(8,:), RVE_Frame_Points(7,:), N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 1);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(2,:), RVE_Frame_Points(3,:), 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 2);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(11,:), RVE_Frame_Points(10,:), 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 2);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(2,:), RVE_Frame_Points(6,:), N_Elements_Vertical, Next_Node_Number, Next_Element_Number, 3);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(7,:), RVE_Frame_Points(3,:), N_Elements_Vertical, Next_Node_Number, Next_Element_Number, 4);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(10,:), RVE_Frame_Points(6,:), N_Elements_Vertical, Next_Node_Number, Next_Element_Number, 4);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(7,:), RVE_Frame_Points(11,:), N_Elements_Vertical, Next_Node_Number, Next_Element_Number, 3);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                if (strcmp(Cell_Filler, 'TRUE'))
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(1,:), RVE_Frame_Points(5,:), RVE_Frame_Points(2,:), RVE_Frame_Points(6,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(5,:), RVE_Frame_Points(9,:), RVE_Frame_Points(6,:), RVE_Frame_Points(10,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(3,:), RVE_Frame_Points(7,:), RVE_Frame_Points(4,:), RVE_Frame_Points(8,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(7,:), RVE_Frame_Points(11,:), RVE_Frame_Points(8,:), RVE_Frame_Points(12,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(2,:), RVE_Frame_Points(6,:), RVE_Frame_Points(3,:), RVE_Frame_Points(7,:), N_Elements_Vertical, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(6,:), RVE_Frame_Points(10,:), RVE_Frame_Points(7,:), RVE_Frame_Points(11,:), N_Elements_Vertical, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5); %#ok<ASGLU>

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                end
                
            case 'TYPE_B'
                
                % Число элементов на вертикальных и горизонтальных линиях ячейки
                N_Elements_Horizontal = N_Elements_Edge;
                N_Elements_Vertical = N_Elements_Edge;
                
                % Контроль соотношения сторон элементов в наполнителе
                if (strcmp(Cell_Filler, 'TRUE'))
                    
                    Element_Ratio = (norm(RVE_Frame_Points(2,:) - RVE_Frame_Points(6,:)) / N_Elements_Vertical) / ...
                                    (norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(2,:)) / N_Elements_Horizontal);
                    
                    if (Element_Ratio > Element_Critical_Ratio)
                        N_Elements_Vertical = ceil(N_Elements_Vertical * Element_Ratio / Element_Critical_Ratio);
                    end
                    
                    Element_Ratio = (norm(RVE_Frame_Points(5,:) - RVE_Frame_Points(6,:)) / N_Elements_Horizontal) / ...
                                    (norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(5,:)) / N_Elements_Vertical);
                    
                    if (Element_Ratio > Element_Critical_Ratio)
                        N_Elements_Horizontal = ceil(N_Elements_Horizontal * Element_Ratio / Element_Critical_Ratio);
                    end
                    
                end
                
                % Минимальная длина ребра элемента
                Min_Element_Edge = min([norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(2,:)) / N_Elements_Horizontal, ...
                                        norm(RVE_Frame_Points(9,:) - RVE_Frame_Points(10,:)) / N_Elements_Horizontal, ...
                                        norm(RVE_Frame_Points(2,:) - RVE_Frame_Points(10,:)) / N_Elements_Vertical, ...
                                        norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(9,:)) / N_Elements_Vertical]);
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(9,:), RVE_Frame_Points(10,:), N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 2);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(11,:), RVE_Frame_Points(12,:), N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 2);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(8,:), RVE_Frame_Points(7,:), N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 2);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(6,:), RVE_Frame_Points(5,:), N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 2);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(3,:), RVE_Frame_Points(2,:), 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 1);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(2,:), RVE_Frame_Points(10,:), N_Elements_Vertical, Next_Node_Number, Next_Element_Number, 4);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(2,:), RVE_Frame_Points(6,:), N_Elements_Vertical, Next_Node_Number, Next_Element_Number, 3);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(7,:), RVE_Frame_Points(3,:), N_Elements_Vertical, Next_Node_Number, Next_Element_Number, 4);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Line(RVE_Frame_Points(11,:), RVE_Frame_Points(3,:), N_Elements_Vertical, Next_Node_Number, Next_Element_Number, 3);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                if (strcmp(Cell_Filler, 'TRUE'))
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(9,:), RVE_Frame_Points(1,:), RVE_Frame_Points(10,:), RVE_Frame_Points(2,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(1,:), RVE_Frame_Points(5,:), RVE_Frame_Points(2,:), RVE_Frame_Points(6,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(11,:), RVE_Frame_Points(3,:), RVE_Frame_Points(12,:), RVE_Frame_Points(4,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(3,:), RVE_Frame_Points(7,:), RVE_Frame_Points(4,:), RVE_Frame_Points(8,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(10,:), RVE_Frame_Points(2,:), RVE_Frame_Points(11,:), RVE_Frame_Points(3,:), N_Elements_Vertical, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(2,:), RVE_Frame_Points(6,:), RVE_Frame_Points(3,:), RVE_Frame_Points(7,:), N_Elements_Vertical, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5); %#ok<ASGLU>

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                end
                
            otherwise
                
                fprintf (1, '\nОшибка функции Mesher_2D_Type_3: недопустимое значение параметра RVE_Type !\n');
                ErrCode = -1;
                return;
            
        end
        
    case 'SOLID'
        
        switch RVE_Type
            
            case 'TYPE_A'
                
                % Число элементов на вертикальных и горизонтальных линиях ячейки
                N_Elements_Horizontal = max(N_Elements_Edge, ceil(norm(RVE_Frame_Points(31,:) - RVE_Frame_Points(3,:)) / Foil_Thickness / Element_Critical_Ratio));
                N_Elements_Vertical = max(N_Elements_Edge, ceil(norm(RVE_Frame_Points(30,:) - RVE_Frame_Points(12,:)) / Foil_Thickness / Element_Critical_Ratio));
                
                % Контроль соотношения сторон элементов в наполнителе
                if (strcmp(Cell_Filler, 'TRUE'))
                    
                    Element_Ratio = (norm(RVE_Frame_Points(30,:) - RVE_Frame_Points(12,:)) / N_Elements_Vertical) / ...
                                    (norm(RVE_Frame_Points(44,:) - RVE_Frame_Points(30,:)) / N_Elements_Horizontal);
                    
                    if (Element_Ratio > Element_Critical_Ratio)
                        N_Elements_Vertical = ceil(N_Elements_Vertical * Element_Ratio / Element_Critical_Ratio);
                    end
                    
                    Element_Ratio = (norm(RVE_Frame_Points(9,:) - RVE_Frame_Points(12,:)) / N_Elements_Horizontal) / ...
                                    (norm(RVE_Frame_Points(44,:) - RVE_Frame_Points(9,:)) / N_Elements_Vertical);
                    
                    if (Element_Ratio > Element_Critical_Ratio)
                        N_Elements_Horizontal = ceil(N_Elements_Horizontal * Element_Ratio / Element_Critical_Ratio);
                    end
                    
                end
                
                % Минимальная длина ребра элемента
                Min_Element_Edge = min([norm(RVE_Frame_Points(9,:) - RVE_Frame_Points(12,:)) / N_Elements_Horizontal, ...
                                        norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(2,:)) / N_Elements_Horizontal, ...
                                        norm(RVE_Frame_Points(30,:) - RVE_Frame_Points(12,:)) / N_Elements_Vertical, ...
                                        norm(RVE_Frame_Points(44,:) - RVE_Frame_Points(9,:)) / N_Elements_Vertical, ...
                                        norm(RVE_Frame_Points(9,:) - RVE_Frame_Points(10,:)), ...
                                        norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(44,:))]);
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(10,:), RVE_Frame_Points(34,:), RVE_Frame_Points(9,:), RVE_Frame_Points(12,:), N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(12,:), RVE_Frame_Points(13,:), RVE_Frame_Points(34,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(11,:), RVE_Frame_Points(14,:), RVE_Frame_Points(10,:), RVE_Frame_Points(34,:), N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(34,:), RVE_Frame_Points(13,:), RVE_Frame_Points(14,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(37,:), RVE_Frame_Points(20,:), RVE_Frame_Points(16,:), RVE_Frame_Points(19,:), N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(16,:), RVE_Frame_Points(37,:), RVE_Frame_Points(17,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(18,:), RVE_Frame_Points(21,:), RVE_Frame_Points(37,:), RVE_Frame_Points(20,:), N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(17,:), RVE_Frame_Points(37,:), RVE_Frame_Points(18,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(6,:), RVE_Frame_Points(8,:), RVE_Frame_Points(31,:), RVE_Frame_Points(32,:), 2*N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(2,:), RVE_Frame_Points(31,:), RVE_Frame_Points(6,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(32,:), RVE_Frame_Points(4,:), RVE_Frame_Points(8,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(41,:), RVE_Frame_Points(42,:), RVE_Frame_Points(22,:), RVE_Frame_Points(24,:), 2*N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(22,:), RVE_Frame_Points(41,:), RVE_Frame_Points(26,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(24,:), RVE_Frame_Points(28,:), RVE_Frame_Points(42,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(35,:), RVE_Frame_Points(6,:), RVE_Frame_Points(12,:), RVE_Frame_Points(30,:), N_Elements_Vertical, 1, Next_Node_Number, Next_Element_Number, 7);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(12,:), RVE_Frame_Points(35,:), RVE_Frame_Points(13,:), Next_Node_Number, Next_Element_Number, 7);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(2,:), RVE_Frame_Points(6,:), RVE_Frame_Points(30,:), Next_Node_Number, Next_Element_Number, 7);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(43,:), RVE_Frame_Points(18,:), RVE_Frame_Points(24,:), RVE_Frame_Points(39,:), N_Elements_Vertical, 1, Next_Node_Number, Next_Element_Number, 7);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(43,:), RVE_Frame_Points(28,:), RVE_Frame_Points(24,:), Next_Node_Number, Next_Element_Number, 7);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(17,:), RVE_Frame_Points(18,:), RVE_Frame_Points(39,:), Next_Node_Number, Next_Element_Number, 7);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(8,:), RVE_Frame_Points(38,:), RVE_Frame_Points(33,:), RVE_Frame_Points(16,:), N_Elements_Vertical, 1, Next_Node_Number, Next_Element_Number, 8);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(4,:), RVE_Frame_Points(33,:), RVE_Frame_Points(8,:), Next_Node_Number, Next_Element_Number, 8);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(16,:), RVE_Frame_Points(17,:), RVE_Frame_Points(38,:), Next_Node_Number, Next_Element_Number, 8);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(14,:), RVE_Frame_Points(40,:), RVE_Frame_Points(36,:), RVE_Frame_Points(22,:), N_Elements_Vertical, 1, Next_Node_Number, Next_Element_Number, 8);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(13,:), RVE_Frame_Points(36,:), RVE_Frame_Points(14,:), Next_Node_Number, Next_Element_Number, 8);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(22,:), RVE_Frame_Points(26,:), RVE_Frame_Points(40,:), Next_Node_Number, Next_Element_Number, 8);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                if (strcmp(Cell_Filler, 'TRUE'))
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(44,:), RVE_Frame_Points(9,:), RVE_Frame_Points(30,:), RVE_Frame_Points(12,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(1,:), RVE_Frame_Points(44,:), RVE_Frame_Points(2,:), RVE_Frame_Points(30,:), 1, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(11,:), RVE_Frame_Points(46,:), RVE_Frame_Points(14,:), RVE_Frame_Points(40,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(46,:), RVE_Frame_Points(25,:), RVE_Frame_Points(40,:), RVE_Frame_Points(26,:), 1, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(33,:), RVE_Frame_Points(16,:), RVE_Frame_Points(45,:), RVE_Frame_Points(19,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(4,:), RVE_Frame_Points(33,:), RVE_Frame_Points(5,:), RVE_Frame_Points(45,:), 1, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(18,:), RVE_Frame_Points(43,:), RVE_Frame_Points(21,:), RVE_Frame_Points(47,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(43,:), RVE_Frame_Points(28,:), RVE_Frame_Points(47,:), RVE_Frame_Points(29,:), 1, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(6,:), RVE_Frame_Points(35,:), RVE_Frame_Points(8,:), RVE_Frame_Points(38,:), N_Elements_Vertical, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(35,:), RVE_Frame_Points(13,:), RVE_Frame_Points(38,:), RVE_Frame_Points(17,:), 1, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(36,:), RVE_Frame_Points(22,:), RVE_Frame_Points(39,:), RVE_Frame_Points(24,:), N_Elements_Vertical, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(13,:), RVE_Frame_Points(36,:), RVE_Frame_Points(17,:), RVE_Frame_Points(39,:), 1, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5); %#ok<ASGLU>

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                end
                
            case 'TYPE_B'
                
                % Число элементов на вертикальных и горизонтальных линиях ячейки
                N_Elements_Horizontal = max(N_Elements_Edge, ceil(norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(30,:)) / Foil_Thickness / Element_Critical_Ratio));
                N_Elements_Vertical = max(N_Elements_Edge, ceil(norm(RVE_Frame_Points(31,:) - RVE_Frame_Points(10,:)) / Foil_Thickness / Element_Critical_Ratio));
                
                % Контроль соотношения сторон элементов в наполнителе
                if (strcmp(Cell_Filler, 'TRUE'))
                    
                    Element_Ratio = (norm(RVE_Frame_Points(36,:) - RVE_Frame_Points(22,:)) / N_Elements_Vertical) / ...
                                    (norm(RVE_Frame_Points(36,:) - RVE_Frame_Points(46,:)) / N_Elements_Horizontal);
                    
                    if (Element_Ratio > Element_Critical_Ratio)
                        N_Elements_Vertical = ceil(N_Elements_Vertical * Element_Ratio / Element_Critical_Ratio);
                    end
                    
                    Element_Ratio = (norm(RVE_Frame_Points(21,:) - RVE_Frame_Points(22,:)) / N_Elements_Horizontal) / ...
                                    (norm(RVE_Frame_Points(21,:) - RVE_Frame_Points(46,:)) / N_Elements_Vertical);
                    
                    if (Element_Ratio > Element_Critical_Ratio)
                        N_Elements_Horizontal = ceil(N_Elements_Horizontal * Element_Ratio / Element_Critical_Ratio);
                    end
                    
                end
                
                % Минимальная длина ребра элемента
                Min_Element_Edge = min([norm(RVE_Frame_Points(6,:) - RVE_Frame_Points(7,:)) / N_Elements_Horizontal, ...
                                        norm(RVE_Frame_Points(13,:) - RVE_Frame_Points(19,:)) / N_Elements_Horizontal, ...
                                        norm(RVE_Frame_Points(7,:) - RVE_Frame_Points(35,:)) / N_Elements_Vertical, ...
                                        norm(RVE_Frame_Points(6,:) - RVE_Frame_Points(44,:)) / N_Elements_Vertical, ...
                                        norm(RVE_Frame_Points(1,:) - RVE_Frame_Points(6,:)), ...
                                        norm(RVE_Frame_Points(44,:) - RVE_Frame_Points(19,:))]);
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(6,:), RVE_Frame_Points(7,:), RVE_Frame_Points(1,:), RVE_Frame_Points(30,:), N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(30,:), RVE_Frame_Points(2,:), RVE_Frame_Points(7,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(8,:), RVE_Frame_Points(9,:), RVE_Frame_Points(32,:), RVE_Frame_Points(5,:), N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(4,:), RVE_Frame_Points(32,:), RVE_Frame_Points(8,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(25,:), RVE_Frame_Points(40,:), RVE_Frame_Points(21,:), RVE_Frame_Points(22,:), N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(22,:), RVE_Frame_Points(26,:), RVE_Frame_Points(40,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(43,:), RVE_Frame_Points(29,:), RVE_Frame_Points(23,:), RVE_Frame_Points(24,:), N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(23,:), RVE_Frame_Points(43,:), RVE_Frame_Points(28,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(34,:), RVE_Frame_Points(37,:), RVE_Frame_Points(10,:), RVE_Frame_Points(12,:), 2*N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(10,:), RVE_Frame_Points(34,:), RVE_Frame_Points(13,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(12,:), RVE_Frame_Points(15,:), RVE_Frame_Points(37,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(16,:), RVE_Frame_Points(18,:), RVE_Frame_Points(34,:), RVE_Frame_Points(37,:), 2*N_Elements_Horizontal, 1, Next_Node_Number, Next_Element_Number, 6);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(34,:), RVE_Frame_Points(16,:), RVE_Frame_Points(13,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(37,:), RVE_Frame_Points(15,:), RVE_Frame_Points(18,:), Next_Node_Number, Next_Element_Number, 6);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(41,:), RVE_Frame_Points(16,:), RVE_Frame_Points(22,:), RVE_Frame_Points(36,:), N_Elements_Vertical, 1, Next_Node_Number, Next_Element_Number, 7);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(41,:), RVE_Frame_Points(26,:), RVE_Frame_Points(22,:), Next_Node_Number, Next_Element_Number, 7);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(16,:), RVE_Frame_Points(36,:), RVE_Frame_Points(13,:), Next_Node_Number, Next_Element_Number, 7);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(38,:), RVE_Frame_Points(8,:), RVE_Frame_Points(12,:), RVE_Frame_Points(33,:), N_Elements_Vertical, 1, Next_Node_Number, Next_Element_Number, 7);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(38,:), RVE_Frame_Points(15,:), RVE_Frame_Points(12,:), Next_Node_Number, Next_Element_Number, 7);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(4,:), RVE_Frame_Points(8,:), RVE_Frame_Points(33,:), Next_Node_Number, Next_Element_Number, 7);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(7,:), RVE_Frame_Points(35,:), RVE_Frame_Points(31,:), RVE_Frame_Points(10,:), N_Elements_Vertical, 1, Next_Node_Number, Next_Element_Number, 8);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(2,:), RVE_Frame_Points(31,:), RVE_Frame_Points(7,:), Next_Node_Number, Next_Element_Number, 8);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(10,:), RVE_Frame_Points(13,:), RVE_Frame_Points(35,:), Next_Node_Number, Next_Element_Number, 8);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(18,:), RVE_Frame_Points(42,:), RVE_Frame_Points(39,:), RVE_Frame_Points(23,:), N_Elements_Vertical, 1, Next_Node_Number, Next_Element_Number, 8);

                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(15,:), RVE_Frame_Points(39,:), RVE_Frame_Points(18,:), Next_Node_Number, Next_Element_Number, 8);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Tria_Element(RVE_Frame_Points(23,:), RVE_Frame_Points(28,:), RVE_Frame_Points(42,:), Next_Node_Number, Next_Element_Number, 8);
                
                NODES_2D = [NODES_2D; New_Nodes];
                ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                
                if (strcmp(Cell_Filler, 'TRUE'))
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(6,:), RVE_Frame_Points(44,:), RVE_Frame_Points(7,:), RVE_Frame_Points(35,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(44,:), RVE_Frame_Points(19,:), RVE_Frame_Points(35,:), RVE_Frame_Points(13,:), 1, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(46,:), RVE_Frame_Points(21,:), RVE_Frame_Points(36,:), RVE_Frame_Points(22,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(19,:), RVE_Frame_Points(46,:), RVE_Frame_Points(13,:), RVE_Frame_Points(36,:), 1, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(8,:), RVE_Frame_Points(38,:), RVE_Frame_Points(9,:), RVE_Frame_Points(45,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(38,:), RVE_Frame_Points(15,:), RVE_Frame_Points(45,:), RVE_Frame_Points(20,:), 1, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(39,:), RVE_Frame_Points(23,:), RVE_Frame_Points(47,:), RVE_Frame_Points(24,:), N_Elements_Vertical, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(15,:), RVE_Frame_Points(39,:), RVE_Frame_Points(20,:), RVE_Frame_Points(47,:), 1, N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(31,:), RVE_Frame_Points(10,:), RVE_Frame_Points(33,:), RVE_Frame_Points(12,:), N_Elements_Vertical, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(2,:), RVE_Frame_Points(31,:), RVE_Frame_Points(4,:), RVE_Frame_Points(33,:), 1, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(16,:), RVE_Frame_Points(41,:), RVE_Frame_Points(18,:), RVE_Frame_Points(42,:), N_Elements_Vertical, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5);

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                    [New_Nodes, New_Elements, Next_Node_Number, Next_Element_Number] = Mesh_2D_Quadrilateral(RVE_Frame_Points(41,:), RVE_Frame_Points(26,:), RVE_Frame_Points(42,:), RVE_Frame_Points(28,:), 1, 2*N_Elements_Horizontal, Next_Node_Number, Next_Element_Number, 5); %#ok<ASGLU>

                    NODES_2D = [NODES_2D; New_Nodes];
                    ELEMENTS_2D = [ELEMENTS_2D; New_Elements];
                    
                end
                
            otherwise
                
                fprintf (1, '\nОшибка функции Mesher_2D_Type_3: недопустимое значение параметра RVE_Type !\n');
                ErrCode = -1;
                return;
            
        end
        
    otherwise
        
        fprintf (1, '\nОшибка функции Mesher_2D_Type_3: недопустимое значение параметра Cell_Mesh_Type !\n');
        ErrCode = -1;
        return;
    
end

% Допуск при слиянии узлов
Node_Merge_Tolerance = Min_Element_Edge * Merge_Tolerance_Multiplier;

% Слияние и перенумерация узлов
[NODES_2D, ELEMENTS_2D] = Merge_Nodes(NODES_2D, ELEMENTS_2D, Node_Merge_Tolerance);

% Рекомендация для числа элементов по высоте ячейки
if ((N_Elements_Vertical == 1) && (N_Elements_Horizontal == 1))
    N_Layers_Suggestion = 1;
    return;
end

RVE_Length_X = max(RVE_Frame_Points(:,1)) - min(RVE_Frame_Points(:,1));
RVE_Length_Y = max(RVE_Frame_Points(:,2)) - min(RVE_Frame_Points(:,2));

if (RVE_Length_Y <= RVE_Length_X)
    N_Layers_Suggestion = 2*N_Elements_Vertical;
else
    N_Layers_Suggestion = 4*N_Elements_Horizontal;
end
